﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeDynamiksEngine;

using MESCodeFile;

namespace mes_tublay
{
    public partial class frmMayorApproval : Form
    {
        CodeScript script;
        CodeProcessor proc = new CodeProcessor(ThreadingMode.Normal);
        DataCache dat = new DataCache("myData");
        int userToView = -1;
        string[] metaData = new string[2];
        string fImageName = "";
        bool hasImage = false;

        updateInfo bProfile = new updateInfo();
        string applicationNumber, referenceNumber, businessReferenceNumber, previousStatus;
        string status = "";
        int currentBusPermitNum;
        updateInfo businessPermit = new updateInfo();

        DataTable tblbusinessfeesandcharges, tblmayorpermitfee;
        DataTable result;
        DataTable tblBusinessAssessment, tblBusinessInformation;
        bool isSearching = false;

        public frmMayorApproval()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
        }

        public void getApplication(string app, string refNo)
        {
            applicationNumber = app;
            referenceNumber = refNo;
            isSearching = false;
            metaData = new string[2];
            metaData[0] = app;
            metaData[1] = refNo;
        }

        void proc_ProcessCompleted(object sender, EventArgs e)
        {
            proc.SafeCall(this, delegate
            {
                if (proc.State == ProcessorFlag.Error ||
                    proc.State == ProcessorFlag.DataRdyErr ||
                    proc.State == ProcessorFlag.ReadyErr)
                {
                    MessageBox.Show(proc.GetProcessorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    proc.ClearErrorFlag();
                    return;
                }
                switch (proc.GetLastProcessName().ToString())
                {
                    case "COMMIT":
                        btnClose_Click(null, null);
                        break;
                    case "GETINFO":
                        tabMayorApproval.SelectedIndex = 0;

                        DataCache cache = new DataCache("MetaData");
                        string businessType = "";
                        proc.DataFetch(out cache);
                        cache.SetActivePage(0);
                        //get business information
                        if (cache.GetRecordCount(0) == 1)
                        {
                            tblBusinessInformation = cache.ExportPage();

                            businessType = cache.GetValue(0, "fBusinessType").ToString();
                            txtDate.Text = DateTime.Parse(cache.GetValue(0, "fDateApplied").ToString()).ToLongDateString();
                            txtApplicationNumber.Text = metaData[0];
                            txtKindOfBusiness.Text = cache.GetValue(0, "fKindOfBusiness").ToString();
                            txtTradeName.Text = cache.GetValue(0, "fTradeName").ToString();

                            //address
                            string houseNumber = cache.GetValue(0, "fHouseNumber").ToString();
                            string bldg = cache.GetValue(0, "fBuildingName").ToString();
                            string unit = cache.GetValue(0, "fUnitNumber").ToString();
                            string street = cache.GetValue(0, "fStreet").ToString();
                            string barangay = cache.GetValue(0, "fBarangay").ToString();
                            string subd = cache.GetValue(0, "fSubdivision").ToString();
                            string municipality = cache.GetValue(0, "fMunicipality").ToString();
                            string prov = cache.GetValue(0, "fProvince").ToString();

                            txtBAddress.Text = houseNumber + " " + bldg + " " + unit + ", " + street + ", " + barangay + ", " + subd + ", " + municipality + ", " + prov;

                            //contact and email
                            string contact = cache.GetValue(0, "fContactNumber").ToString();
                            string email = cache.GetValue(0, "fEmailAddress").ToString();

                            txtBCE.Text = contact + " | " + email;

                            //get the businessReferenceNumber
                            businessReferenceNumber = cache.GetValue(0, "fBusinessReferenceNumber").ToString();
                        }

                        cache.SetActivePage(1);
                        //get owner's profile
                        if (cache.GetRecordCount(1) == 1)
                        {
                            if (businessType.Equals("SINGLE"))
                            {
                                string lastname = cache.GetValue(0, "fLastName").ToString();
                                string firstName = cache.GetValue(0, "fFirstName").ToString();
                                string middleName = cache.GetValue(0, "fMiddleName").ToString();

                                txtName.Text = firstName + " " + middleName + " " + lastname;
                            }
                            else
                            {
                                string companyName = cache.GetValue(0, "fCompanyName").ToString();
                                txtName.Text = companyName;
                            }

                            //address
                            string houseNumber = cache.GetValue(0, "fHouseNumber").ToString();
                            string bldg = cache.GetValue(0, "fBuildingName").ToString();
                            string unit = cache.GetValue(0, "fUnitNumber").ToString();
                            string street = cache.GetValue(0, "fStreet").ToString();
                            string barangay = cache.GetValue(0, "fBarangay").ToString();
                            string subd = cache.GetValue(0, "fSubdivision").ToString();
                            string municipality = cache.GetValue(0, "fMunicipality").ToString();
                            string prov = cache.GetValue(0, "fProvince").ToString();

                            txtOAddress.Text = houseNumber + " " + bldg + " " + unit + ", " + street + ", " + barangay +  ", " + subd +  ", " + municipality +  ", " + prov;

                            //contact and email
                            string contact = cache.GetValue(0, "fContactNumber").ToString();
                            string email = cache.GetValue(0, "fEmailAddress").ToString();
                            fImageName = cache.GetValue(0, "fImgName").ToString();
                            txtOCE.Text = contact + " | " + email;

                        }

                        cache.SetActivePage(2);
                        tblbusinessfeesandcharges = cache.ExportPage();
                        cache.SetActivePage(3);
                        tblmayorpermitfee = cache.ExportPage();
                        cache.SetActivePage(4);
                        tblBusinessAssessment = cache.ExportPage();

                        joinTable();

                        grdAssessment.DataSource = tblBusinessAssessment;
                        grdAssessment.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        grdAssessment.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        grdAssessment.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

                        for (int i = 0; i < grdAssessment.Columns.Count; i++)
                        {
                            grdAssessment.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                        }

                        cache.SetActivePage(5);
                        if(cache.GetRecordCount(5) > 0)
                        {
                            bProfile = new updateInfo();
                            bProfile.fID = cache.GetValue(0, 0).ToString();
                            bProfile.fStamp = DateTime.Parse(cache.GetValue(0, 1).ToString());
                            previousStatus = cache.GetValue(0, 2).ToString();
                        }

                        cache.SetActivePage(6);
                        
                        currentBusPermitNum = int.Parse(cache.GetValue(0, "fCurNum").ToString());
                        
                        businessPermit.fID = cache.GetValue(0, "fID").ToString();
                        businessPermit.fStamp = DateTime.Parse(cache.GetValue(0, "fStamp").ToString());

                        lblBusinessPermitNumber.Text = "TMBP"+ currentBusPermitNum.ToString("0000") + "-"+DateTime.Now.Year.ToString() ;

                        //additional
                        if (!fImageName.Equals(""))
                        {
                            hasImage = true;
                            CodeScript s = new CodeScript("GETIMAGE");
                            s.Add(CodeIns.RFU_NEW_CORE);
                            s.Add(CodeIns.RFU_SET_CONFIG, "ftp://" + Common.CFG.Read("SERVER"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), 256);
                            s.Add(CodeIns.RFU_DWLD, Common.LOCPATH + CurrentUser.USERIDNUMBER + Const.APPLICATION_EBPLS_IMG, fImageName);
                            s.Add(CodeIns.RFU_END);
                            proc.SetOnErrorBehavior(OnErrorBehavior.ResumeNext);
                            proc.Run(s);
                        }
                            //grdMayor.DataSource = result;
                        break;
                    case "GETMETADATA":
                        cache = new DataCache("MetaData");
                        proc.DataFetch(out cache);
                        cache.SetActivePage(0);
                        if (cache.GetRecordCount(0) == 1)
                        {
                            string data = cache.GetValue(0,0).ToString();
                            metaData = data.Split('|');
                            loadInformation();
                        }
                        break;
                    case "GETIMAGE":
                        //DataCache cachepriv = new DataCache("img");
                        //proc.DataFetch(out cachepriv);
                        //cachepriv.SetActivePage(0);
                        if (hasImage)
                        {
                            try
                            {
                                using (System.IO.Stream fs = System.IO.File.Open(Common.LOCPATH + CurrentUser.USERIDNUMBER + Const.APPLICATION_EBPLS_IMG, System.IO.FileMode.Open))
                                {
                                    imgApplicant.Image = new Bitmap(fs);
                                    fs.Close();
                                }
                            }
                            catch (Exception)
                            {
                                Confirm.Error("User Image", "There was a problem loading your user image.");
                                imgApplicant.Image = new Bitmap(480, 480);
                            }
                        }
                        break;
                    case "LOAD":
                        proc.DataFetch(out dat);
                        FetchData();
                        if(!isSearching)
                            loadInformation();
                        break;
                }
            }); 
        }

        private void InitializeForm()
        {
            result = new DataTable();
            result.Columns.Add("Local Taxes");
            result.Columns.Add("Code");
            result.Columns.Add("Amount Due");
            result.Columns.Add("Penalty");
            result.Columns.Add("Total");

            grdMayor.DataSource = result;

           
            for (int i = 0; i < grdMayor.Columns.Count; i++)
            {
                grdMayor.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            proc.ProcessCompleted += new EventHandler(proc_ProcessCompleted);
            grdApplicants.Click += new EventHandler(grdApplicants_Click);            
            lstApplicationNumber.DoubleClick += new EventHandler(lstApplicationNumber_DoubleClick);
            btnApprove.Click += new EventHandler(btnApprove_Click);
            btnDisapprove.Click += new EventHandler(btnDisapprove_Click);
            txtSearch.KeyPress += new KeyPressEventHandler(txtSearch_KeyPress);
            loadPulled();
        }

        void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtSearch.Text != "")
            {
                isSearching = true;
                loadPulled();
            }
        }

        void grdApplicants_Click(object sender, EventArgs e)
        {
            if (grdApplicants.SelectedRows.Count == 1)
            {
                metaData[0] = grdApplicants.SelectedRows[0].Cells[3].Value.ToString();
                metaData[1] = grdApplicants.SelectedRows[0].Cells[0].Value.ToString();
                loadInformation();
            }
        }
        void btnDisapprove_Click(object sender, EventArgs e)
        {
            if (txtRemarks.Text != "")
            {
                status = "DISAPPROVED";
                commit();
            }
        }
        void btnApprove_Click(object sender, EventArgs e)
        {
            DialogResult dlg2 = Confirm.Question("Approval Information", "Business permit application will be approve...\r\n Do you wish to continue?");
            if (dlg2 == System.Windows.Forms.DialogResult.No) return;
            status = "APPROVED";
            commit();
        }
        void joinTable()
        {
            DataRow dr;

            for (int i = 0; i < tblmayorpermitfee.Rows.Count; i++)
            {
                dr = result.NewRow();
                dr[0] = tblmayorpermitfee.Rows[i][0].ToString();
                dr[1] = tblmayorpermitfee.Rows[i][1].ToString();
                double amt = 0;
                double.TryParse(tblmayorpermitfee.Rows[i][2].ToString(), out amt);
                dr[2] = amt.ToString("#,#0.00");
                double penalty = 0;
                dr[3] = penalty.ToString("#,#0.00");

                double total = amt + penalty;
                dr[4] = total.ToString("#,#0.00");

                result.Rows.Add(dr);
            }
            
            dr = result.NewRow();
            result.Rows.Add(dr);
            
            dr = result.NewRow();
            dr[0] = "Other Fees";
            result.Rows.Add(dr);
            
            for (int i = 0; i < tblbusinessfeesandcharges.Rows.Count; i++)
            {
                dr = result.NewRow();
                dr[0] = tblbusinessfeesandcharges.Rows[i][0].ToString();
                dr[1] = tblbusinessfeesandcharges.Rows[i][1].ToString();
                double amt = 0;
                double.TryParse(tblbusinessfeesandcharges.Rows[i][2].ToString(), out amt);
                dr[2] = amt.ToString("#,#0.00");
                double penalty = 0;
                double.TryParse(tblbusinessfeesandcharges.Rows[i][3].ToString(), out penalty);
                dr[3] = penalty.ToString("#,#0.00");
                double total = amt + penalty;
                dr[4] = total.ToString("#,#0.00");

                result.Rows.Add(dr);
            }
        }
       
        void lstApplicationNumber_DoubleClick(object sender, EventArgs e)
        {
            if (lstApplicationNumber.SelectedIndices.Count == 0) return;
            dat.SetActivePage(0);
            userToView = lstApplicationNumber.SelectedIndices[0];

            CodeScript s = new CodeScript("GETMETADATA");
            s.Add(CodeIns.SYS_USE_CORE, 0);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                    Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            s.Add(CodeIns.DBU_QUERY, "SELECT fMeta FROM tblNotificationService WHERE fID=" + Sym.PQS(dat.GetValue(userToView, "fID").ToString()));
            s.Add(CodeIns.DBU_END);
            proc.Run(s);

        }
        void FetchData()
        {
            //Obtain the Application Number.
            dat.SetActivePage(0);

            //Add Application Number to ListView control
            lstApplicationNumber.Clear();
            object[] _AppNum = dat.GetObjectList("fMeta");
            for (int i = 0; i < _AppNum.Length; i++)
            {
                lstApplicationNumber.Items.Add(_AppNum[i].ToString(), 1);
            }


            dat.SetActivePage(1);
            /*DataTable tblApplicants = dat.ExportPage();

            DataTable result = tblApplicants.Clone();
            for (int h = 0; h < _AppNum.Length; h++)
            {
                string[] splitData = _AppNum[h].ToString().Split('|');

                for (int i = 0; i < tblApplicants.Rows.Count; i++)
                {

                }
            }*/

            grdApplicants.DataSource = dat.ExportPage();
           
        }
        void loadPulled()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            script = new CodeScript("LOAD");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                       Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);

            script.Add(CodeIns.DBU_QUERY, "SELECT fID, fStamp, fMeta FROM tblNotificationService WHERE fStatus = 'PULLED' AND fMsgType = 'MAYOR_OFFICE_APPROVAL' ORDER BY fStamp DESC");
            script.Add(CodeIns.DBU_QUERY, "SELECT t1.fReferenceNumber AS 'Reference Number',ffDate AS 'Application Date', ffBusinessReferenceNumber AS 'Business Reference Number', ffApplicationNumber AS 'Application Number', ffBusinessName AS 'Business Name', ffStatus AS 'Business Status', fLastName AS 'Last Name', fFirstName AS 'First Name', fMiddleName AS 'Middle Name', fCompanyName AS 'Company Name' FROM tblBusinessOwnerProfile t1," +
                                                    "(SELECT fDateApplied AS ffDate, fBusinessReferenceNumber AS ffBusinessReferenceNumber, fBusinessName AS ffBusinessName, fReferenceNumber, fBusinessStatus AS ffStatus FROM tblBusinessProfile WHERE fBusinessStatus = 'APPROVED' OR fBusinessStatus = 'PENDING') AS t2 " +
                                                    ", (SELECT fApplicationNumber AS ffApplicationNumber, fReferenceNumber FROM tblBusinessInformation GROUP BY fReferenceNumber) AS t3 " +
                                                    " WHERE ((t1.fReferenceNumber = t2.fReferenceNumber) AND (t1.fReferenceNumber = t3.fReferenceNumber)) AND ((fLastName LIKE '%" + txtSearch.Text + "%') OR (fFirstName LIKE '%" + txtSearch.Text + "%') OR (fMiddleName LIKE '%" + txtSearch.Text + "%')" +
                                                    " OR (ffBusinessName LIKE '%" + txtSearch.Text + "%'))"
                                                    );
            script.Add(CodeIns.DBU_END);
            proc.Run(script);
        }
        void loadInformation()
        {
            if (metaData.Length > 1)
            {
                CodeScript s = new CodeScript("GETINFO");
                s.Add(CodeIns.SYS_USE_CORE, 0);
                s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                        Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
                s.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblBusinessInformation WHERE fApplicationNumber  ='" + metaData[0] + "' ORDER BY fStamp DESC LIMIT 0,1");
                s.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblBusinessOwnerProfile WHERE fReferenceNumber ='" + metaData[1] + "'");
                s.Add(CodeIns.DBU_QUERY, "SELECT fTypeOfServices, fCode, fFee, fPenalty FROM tblBusinessPermitFeesAndCharges WHERE fApplicationNumber = '" + metaData[0] + "'");
                s.Add(CodeIns.DBU_QUERY, "SELECT fItem, fCode, fFee FROM tblMayorPermitFeeAssessment WHERE fApplicationNumber = '" + metaData[0] + "'");
                s.Add(CodeIns.DBU_QUERY, "SELECT fCode AS 'Code', fLineOfBusiness AS 'Line of Business', fNoOfUnits AS 'No. Of Units', fCapitalization AS 'Capitalization', fEssential AS 'Gross Sales -Essential', fNonEssential AS 'Gross Sales -Non-Essentials' FROM tblBusinessAssessment WHERE fApplicationNumber = '" + metaData[0] + "'");
                s.Add(CodeIns.DBU_QUERY, "SELECT ffID, ffStamp, ffStatus, t1.fBusinessReferenceNumber, fApplicationNumber FROM tblBusinessInformation AS t1, (SELECT fBusinessReferenceNumber, fID AS ffID, fStamp AS ffStamp, fBusinessStatus AS ffStatus FROM tblBusinessProfile) AS t2 WHERE (t1.fBusinessReferenceNumber = t2.fBusinessReferenceNumber) AND fApplicationNumber = '"+ metaData[0] + "'");
                s.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblGeneratedBusinessPermitNum");
                s.Add(CodeIns.DBU_END);
                proc.Run(s);
            }
        }
        void commit()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            string sysDate = DateTime.Now.ToString("yyyy-MM-dd");
            script = new CodeScript("COMMIT");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                       Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);

            //update business profile
            if (!previousStatus.Equals("APPROVED"))
            {
                script.Add(CodeIns.DBU_UPDATE, "tblbusinessprofile",
                   new string[]
                {
                    "fBusinessStatus"
                },
                   new object[]
                {
                    "'" + status + "'",
                    
                }, "fID='" + bProfile.fID + "' AND fStamp ='" + bProfile.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            }
            else
            {
                script.Add(CodeIns.DBU_UPDATE, "tblGeneratedBusinessPermitNum", new string[] { "fCurNum" }, new object[] { "fCurNum+1" }, "fID='" + businessPermit.fID + "' AND fStamp = '" + businessPermit.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            }
            //application form information
            script.Add(CodeIns.DBU_INSERT, "tblbusinessinformation",
                new string[]
                {
                    "fDateApplied",         "fBusinessReferenceNumber",         "fReferenceNumber",     
                    "fApplicationNumber",   "fBusinessType",                    "fKindOfOrganization",  
                    "fBusinessStatus",      "fBusinessName",                    "fKindOfBusiness",          
                    "fTradeName",           "fHouseNumber",                     "fBuildingName",
                    "fUnitNumber",          "fStreet",                          "fBarangay",
                    "fSubdivision",         "fMunicipality",                    "fProvince",
                    "fContactNumber",       "fEmailAddress",                    "fPropertyIndexNumber",
                    "fBusinessArea",        "fNumberOfEmployeesInEstablishment","fNumberOfEmployeesInTublay",
                    "fLessorLastName",      "fLessorFirstName",                 "fLessorMiddleName",
                    "fLessorAddress",       "fLessorContactNumber",             "fLessorEmailAddress",
                    "fMonthlyRental",       "fContactLastName",                 "fContactFirstName",
                    "fContactMiddleName",   "fContactTelephoneNumber",          "fContactCellphoneNumber",
                    "fRemarks"
                },
                new object[]
                {
                    "'" + DateTime.Now.ToString("yyyy-MM-dd")+ "'",                             "'" + businessReferenceNumber + "'",                                                "'" + tblBusinessInformation.Rows[0]["fReferenceNumber"].ToString() + "'",
                    "'" + txtApplicationNumber.Text.Trim() + "'",                               "'" + tblBusinessInformation.Rows[0]["fBusinessType"].ToString() + "'",             "'" + tblBusinessInformation.Rows[0]["fKindOfOrganization"].ToString() + "'",
                    "'"+status+"'",                                                             "'" + tblBusinessInformation.Rows[0]["fBusinessName"].ToString() + "'",             "'" + tblBusinessInformation.Rows[0]["fKindOfBusiness"].ToString() + "'",
                    "'" + tblBusinessInformation.Rows[0]["fTradeName"].ToString() + "'",        "'" + tblBusinessInformation.Rows[0]["fHouseNumber"].ToString() + "'",              "'" + tblBusinessInformation.Rows[0]["fBuildingName"].ToString() + "'",
                    "'" + tblBusinessInformation.Rows[0]["fUnitNumber"].ToString() + "'",       "'" + tblBusinessInformation.Rows[0]["fStreet"].ToString() + "'",                   "'" + tblBusinessInformation.Rows[0]["fBarangay"].ToString() + "'",
                    "'" + tblBusinessInformation.Rows[0]["fSubdivision"].ToString() + "'",      "'" + tblBusinessInformation.Rows[0]["fMunicipality"].ToString() + "'",             "'" + tblBusinessInformation.Rows[0]["fProvince"].ToString() + "'",
                    "'" + tblBusinessInformation.Rows[0]["fContactNumber"].ToString() + "'",    "'" + tblBusinessInformation.Rows[0]["fEmailAddress"].ToString() + "'",             "'" + tblBusinessInformation.Rows[0]["fPropertyIndexNumber"].ToString() + "'",
                    "'" + tblBusinessInformation.Rows[0]["fBusinessArea"].ToString() + "'",     "'" + tblBusinessInformation.Rows[0]["fNumberOfEmployeesInEstablishment"].ToString() + "'",     "'" + tblBusinessInformation.Rows[0]["fNumberOfEmployeesInTublay"].ToString() + "'",
                    
                    "'" + tblBusinessInformation.Rows[0]["fLessorLastName"].ToString() + "'",   "'" + tblBusinessInformation.Rows[0]["fLessorFirstName"].ToString() + "'",          "'" + tblBusinessInformation.Rows[0]["fLessorMiddleName"].ToString() + "'",
                    "'" + tblBusinessInformation.Rows[0]["fLessorAddress"].ToString() + "'",    "'" + tblBusinessInformation.Rows[0]["fLessorContactNumber"].ToString() + "'",      "'" + tblBusinessInformation.Rows[0]["fLessorEmailAddress"].ToString() + "'",
                    "'" + tblBusinessInformation.Rows[0]["fMonthlyRental"].ToString() + "'",    "'" + tblBusinessInformation.Rows[0]["fContactLastName"].ToString() + "'",          "'" + tblBusinessInformation.Rows[0]["fContactFirstName"].ToString() + "'",
                    "'" + tblBusinessInformation.Rows[0]["fContactMiddleName"].ToString() + "'","'" + tblBusinessInformation.Rows[0]["fContactTelephoneNumber"].ToString() + "'",   "'" + tblBusinessInformation.Rows[0]["fContactCellphoneNumber"].ToString() + "'",
                    "'"+txtRemarks.Text.Trim()+"'"
                });

            script.Add(CodeIns.DBU_INSERT, "tblBusinessPermit", new string [] {
                "fDateApproved",
                "fApplicationNumber",
                "fOwnerReferenceNumber",
                "fBusinessReferenceNumber",
                "fBusinessPermitNumber"
                }, new object [] {
                "'" + DateTime.Now.ToString("yyyy-MM-dd")+ "'",
                "'" + txtApplicationNumber.Text.Trim() + "'",
                "'" + metaData[1] + "'",
                "'" + businessReferenceNumber + "'",
                "'" + lblBusinessPermitNumber.Text + "'"
                });
                            script.Add(CodeIns.DBU_END);
                            proc.Run(script);
        }
    }
}
